<?php

class Application_Model_DbTable_Floor extends Zend_Db_Table_Abstract
{

    protected $_name = 'floor';

    public function getAll(){
        return $this->fetchAll(null,'abbreviation');
    }

    public function getFloor($id){
        return $this->fetchRow("floor_id = $id");
    }

    public function insertFloor($data){
        $new_data = array(
            'name' => $data['name'],
            'abbreviation' => $data['abbreviation']
        );

        $this->insert($new_data);
    }

    public function updateFloor($id, $data){
        $new_data = array(
            'name' => $data['name'],
            'abbreviation' => $data['abbreviation']
        );

        $this->update($new_data, "floor_id = $id");
    }

    public function deleteFloor($id){
        // Delete associated floor map
        $fmap = realpath('./images/floors').DIRECTORY_SEPARATOR."floor_$id.jpg";
        unlink($fmap);

        // Delete associated venues' picture
        $venue_db = new Application_Model_DbTable_Venue();
        foreach($venue_db->getVenues($id) as $val){
            $vid = $val['venue_id'];
            $vdir = realpath('./images/venues').DIRECTORY_SEPARATOR;
            unlink($vdir.'venue_'.$vid.'_main.jpg');
            unlink($vdir.'venue_'.$vid.'_1.jpg');
            unlink($vdir.'venue_'.$vid.'_2.jpg');
            unlink($vdir.'venue_'.$vid.'_3.jpg');
        }

        // Delete floor from database
        $this->delete("floor_id = $id");
    }
}

